package com.kl.modular.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.kl.common.exception.SystemException;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.common.util.UUIDUtil;
import com.kl.modular.system.dao.bo.SystemRoleBo;
import com.kl.modular.system.dao.mapper.SystemRoleMapper;
import com.kl.modular.system.dao.model.SystemRole;
import com.kl.modular.system.dao.model.SystemRoleAuth;
import com.kl.modular.system.service.api.ISystemRoleAuthService;
import com.kl.modular.system.service.api.ISystemRoleService;
import com.kl.modular.system.service.constant.SystemConStant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色serviceImpl业务实现
 * @author XRP_ZJB
 * @date 2016-08-05 22:11
 * @(C) XIGNSOFT Copyright 
 */
@Service
public class SystemRoleServiceImpl implements ISystemRoleService {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemRoleServiceImpl.class);
	
	@Autowired
	private SystemRoleMapper systemRoleMapper;
	
	@Autowired
	private ISystemRoleAuthService systemRoleAuthService;
	
	/**
	 * 角色列表分页
	 */
	public Pager<SystemRole> getListByBoPage(SystemRoleBo systemRoleBo, PageParameter pageParameter){
		pageParameter.setPageBoolean(true);
		List<SystemRole> systemRoleList = systemRoleMapper.getListByBoPage(systemRoleBo, pageParameter);
		return new Pager<SystemRole>(pageParameter, systemRoleList);
	}
	
	/**
	 * 添加角色
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addSystemRole(SystemRole systemRole ,String authResources) throws Exception {
		String[] authResourcestrs = authResources.split(",");
		if(authResourcestrs == null || authResourcestrs.length == 0){
			logger.error("添加角色发生错误>>authResourcestrs is null");
			throw new SystemException("角色权限为空，或者格式不对，请重新选择");
		}
		for(int i = 0;i < authResourcestrs.length;i++ ){
			String authResource = authResourcestrs[i];
			if(StringUtils.isEmpty(authResource)){
				continue;
			}
			SystemRoleAuth systemRoleAuth = new SystemRoleAuth();
			systemRoleAuth.setRaCode(UUIDUtil.getUniquekey());
			systemRoleAuth.setAuthCode(authResource);
			systemRoleAuth.setRoleCode(systemRole.getRoleCode());
			systemRoleAuth.setStatus(SystemConStant.SYSTEM_ROLE_AUTH_NORMAL_STATUS);
			systemRoleAuthService.addSystemRoleAuth(systemRoleAuth);
		}
		int result = systemRoleMapper.insert(systemRole);
		if(result == 0){
			logger.error("添加角色发生错误>>data:" + JSON.toJSONString(systemRole));
			throw new SystemException("添加角色发生错误");
		}
	}

	/**
	 * 更新角色
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateByCode(SystemRole systemRole ,String authResources) throws Exception {
		// 刪除之前角色权限
		systemRoleAuthService.deleteWlSystemRoleAuth(systemRole.getRoleCode());
		String[] authResourcestrs = authResources.split(",");
		if(authResourcestrs == null || authResourcestrs.length == 0){
			logger.error("添加角色发生错误>>authResourcestrs is null");
			throw new SystemException("角色权限为空，或者格式不对，请重新选择");
		}
		for(int i = 0;i < authResourcestrs.length;i++ ){
			String authResource = authResourcestrs[i];
			if(StringUtils.isEmpty(authResource)){
				continue;
			}
			SystemRoleAuth systemRoleAuth = new SystemRoleAuth();
			systemRoleAuth.setRaCode(UUIDUtil.getUniquekey());
			systemRoleAuth.setAuthCode(authResource);
			systemRoleAuth.setRoleCode(systemRole.getRoleCode());
			systemRoleAuth.setStatus(SystemConStant.SYSTEM_ROLE_AUTH_NORMAL_STATUS);
			systemRoleAuthService.addSystemRoleAuth(systemRoleAuth);
		}
		int result = systemRoleMapper.updateByPrimaryKey(systemRole);
		if(result ==0){
			logger.error("更新角色发生错误>>data:" + JSON.toJSONString(systemRole));
			throw new SystemException("更新角色发生错误");
		}
	}
	
	/**
	 * 删除角色
	 */
	public void deleteByCode(String[] codes) throws Exception {
		int result = systemRoleMapper.deleteByPrimaryKey(codes);
		if(result == 0){
			logger.error("删除角色发生错误>>data:" + JSON.toJSONString(codes));
			throw new SystemException("删除角色发生错误");
		}
	}
	
	/**
	 * 查询角色
	 */
	public SystemRole getSystemRoleByCode(String code) throws Exception {
		SystemRole systemRole = systemRoleMapper.selectByPrimaryKey(code);
		if(systemRole == null){
			logger.error("查询角色发生错误>>data:" + JSON.toJSONString(systemRole));
			throw new SystemException("未查询到角色数据");
		}
		return systemRole;
	}

	/**
	 * 角色列表
	 */
	public List<SystemRole> getListByBo(SystemRoleBo systemRoleBo) {
		PageParameter pageParammeter = new PageParameter();
		pageParammeter.setPageBoolean(false);
		return systemRoleMapper.getListByBoPage(systemRoleBo, pageParammeter);
	}

	/**
	 * 查询角色BY BO
	 */
	public SystemRole getSystemRoleByBo(SystemRoleBo systemRoleBo) {
		return systemRoleMapper.selectByBo(systemRoleBo);
	}
	
}
